/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.business.dao.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.basics.manager.StringManager;
import com.genia.toolbox.persistence.criteria.Criteria;
import com.genia.toolbox.persistence.criteria.CriteriaType;
import com.genia.toolbox.persistence.criteria.PropertyField;
import com.genia.toolbox.persistence.criteria.enumeration.MatchMode;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicValue;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nValue;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Namable;
import com.genia.toolbox.projects.toolbox_basics_project.business.dao.I18nSearchHelperDao;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.LanguageManager;

/**
 * BusinessDao to do i18n search.
 */
public class I18nSearchHelperDaoImpl
    extends AbstractDAO
    implements I18nSearchHelperDao
{

  /**
   * languageManager to know the locale to search on.
   */
  public LanguageManager languageManager;

  /**
   * the {@link StringManager} to use.
   */
  public StringManager stringManager;



  /**
   * Search all T which {@link I18nDynamicValue} propertyField has string value
   * ilike value parameter on current language.
   * 
   * @param <T>
   *                the type of the element having a i18n value
   * @param clazz
   *                the class of the element having a i18n value
   * @param propertyField
   *                to join on
   * @param value
   *                to search on
   * @return a list of T
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public <T> List<T> searchI18nDynamicString(Class<T> clazz, PropertyField<? extends I18nDynamicValue, ? extends I18nDynamicValue> propertyField, String value)
      throws PersistenceException
  {

    Criteria<T> criteria = getCriteria(clazz);
    addI18nDynamicStringRestriction(criteria, criteria.getMainType(), propertyField, value);
    return simpleFind(criteria);

  }



  /**
   * Search all T which {@link I18nDynamicValue} propertyField has string value
   * ilike value parameter on current language.
   * 
   * @param criteria
   *                the base criteria
   * @param criteriaType
   *                the {@link CriteriaType} of the element having a i18n value
   * @param propertyField
   *                to join on
   * @param value
   *                to search on
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public void addI18nDynamicStringRestriction(Criteria<?> criteria, CriteriaType<?> criteriaType, PropertyField<? extends I18nDynamicValue, ? extends I18nDynamicValue> propertyField, String value)
      throws PersistenceException
  {
    if (value == null) {
      return;
    }

    CriteriaType<? extends I18nDynamicValue> i18nDynamicValue = criteria.join(criteriaType, propertyField);
    CriteriaType<? extends I18nValue> i18nValue = criteria.join(i18nDynamicValue, I18nDynamicValue.NAME_I18N_VALUE);

    criteria.addRestriction(equals(getConstantField(getLanguageManager().getCurrentLanguage()), getPropertyField(i18nValue, I18nValue.NAME_LANGUAGE)));
    criteria.addRestriction(ilike(getPropertyField(i18nValue, I18nValue.NAME_ESCAPED_VALUE), getConstantField(getStringManager().removeAccents(value)), MatchMode.ANYWHERE));
  }



  /**
   * Search all T which {@link I18nDynamicValue} propertyField has string value
   * ilike value parameter on current language, with order.
   * 
   * @param <T>
   *                the type of the element having a i18n value
   * @param clazz
   *                the class of the element having a i18n value
   * @param propertyField
   *                to join on
   * @param value
   *                to search on
   * @return a list of T
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public <T> List<T> searchI18nDynamicStringWithOrder(Class<T> clazz, PropertyField<? extends I18nDynamicValue, ? extends I18nDynamicValue> propertyField, String value)
      throws PersistenceException
  {

    Criteria<T> criteria = getCriteria(clazz);
    addI18nDynamicStringRestrictionWithOrder(criteria, criteria.getMainType(), propertyField, value);
    return simpleFind(criteria);

  }



  /**
   * Search all T which {@link I18nDynamicValue} propertyField has string value
   * ilike value parameter on current language, with order.
   * 
   * @param criteria
   *                the base criteria
   * @param criteriaType
   *                the {@link CriteriaType} of the element having a i18n value
   * @param propertyField
   *                to join on
   * @param value
   *                to search on
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public void addI18nDynamicStringRestrictionWithOrder(Criteria<?> criteria, CriteriaType<?> criteriaType, PropertyField<? extends I18nDynamicValue, ? extends I18nDynamicValue> propertyField,
      String value)
      throws PersistenceException
  {
    if (value == null) {
      return;
    }

    CriteriaType<? extends I18nDynamicValue> i18nDynamicValue = criteria.join(criteriaType, propertyField);
    CriteriaType<? extends I18nValue> i18nValue = criteria.join(i18nDynamicValue, I18nDynamicValue.NAME_I18N_VALUE);

    criteria.addRestriction(equals(getConstantField(getLanguageManager().getCurrentLanguage()), getPropertyField(i18nValue, I18nValue.NAME_LANGUAGE)));
    criteria.addRestriction(ilike(getPropertyField(i18nValue, I18nValue.NAME_ESCAPED_VALUE), getConstantField(getStringManager().removeAccents(value)), MatchMode.ANYWHERE));
    criteria.addOrder(asc(getPropertyField(i18nValue, I18nValue.NAME_VALUE)));
  }



  /**
   * add a restriction to the given criteria to retrieve only the value for
   * which the given property on the given criteria type for the given language
   * has the given value.
   * 
   * @param language
   *                the {@link Language} to consider
   * @param criteria
   *                the base criteria
   * @param criteriaType
   *                the {@link CriteriaType} of the element having a i18n value
   * @param propertyField
   *                to join on
   * @param value
   *                to search on
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public void addI18nDynamicStringExactRestriction(Language language, Criteria<?> criteria, CriteriaType<?> criteriaType,
      PropertyField<? extends I18nDynamicValue, ? extends I18nDynamicValue> propertyField, String value)
      throws PersistenceException
  {
    if (value == null) {
      return;
    }

    CriteriaType<? extends I18nDynamicValue> i18nDynamicValue = criteria.join(criteriaType, propertyField);
    CriteriaType<? extends I18nValue> i18nValue = criteria.join(i18nDynamicValue, I18nDynamicValue.NAME_I18N_VALUE);

    criteria.addRestriction(equals(getConstantField(language), getPropertyField(i18nValue, I18nValue.NAME_LANGUAGE)));
    criteria.addRestriction(equals(getPropertyField(i18nValue, I18nValue.NAME_VALUE), getConstantField(value)));
  }



  /**
   * Search all {@link Namable} which {@link I18nDynamicValue} has string value
   * ilike value parameter on current language.
   * 
   * @param <T>
   *                the type of the {@link Namable}
   * @param clazz
   *                which extends {@link Namable}
   * @param value
   *                to search on
   * @return a list of {@link Namable}
   * @throws PersistenceException
   *                 if an error occurs
   */
  @Transactional(readOnly = true)
  public <T extends Namable> List<T> searchNamable(Class<T> clazz, String value)
      throws PersistenceException
  {
    Criteria<T> criteria = getCriteria(clazz);
    addI18nDynamicStringRestriction(criteria, criteria.getMainType(), Namable.NAME_NAME, value);
    return simpleFind(criteria);

  }



  /**
   * getter for the languageManager property.
   * 
   * @return languageManager.
   */
  public LanguageManager getLanguageManager()
  {
    return languageManager;
  }



  /**
   * setter for the languageManager property.
   * 
   * @param languageManager
   *                to set.
   */
  public void setLanguageManager(LanguageManager languageManager)
  {
    this.languageManager = languageManager;
  }



  /**
   * getter for the stringManager property.
   * 
   * @return the stringManager
   */
  public StringManager getStringManager()
  {
    return stringManager;
  }



  /**
   * setter for the stringManager property.
   * 
   * @param stringManager
   *                the stringManager to set
   */
  public void setStringManager(StringManager stringManager)
  {
    this.stringManager = stringManager;
  }

}
